from masonite.controllers import Controller
from masonite.views import View
from masonite.request import Request
from masonite.response import Response
from app.traits.MyControllerTrait import MyControllerTrait
from app.models.AdminConfig import AdminConfig
from masonite.facades import Session
from app.models.AdminActionLog import AdminActionLog
from app.my_package.MyPackage import MyValidator


class AdminConfigController(Controller):
    uri = '/admin/config'
    temp = 'admin.config'

    def index(self, view: View):
        admin_configs = AdminConfig.get_config()
        if admin_configs is not None:
            admin_configs['watermark_switch'] = AdminConfig.FIELDS['watermark_switch'][int(admin_configs['watermark_switch'])]

        uri  = self.uri + '/index'
        data = {
            'uri': self.uri,
            'title': MyControllerTrait.get_permission_title(uri),
            'menus': MyControllerTrait.get_menu_nav(uri),
            'data': admin_configs
        }
        return view.render(self.temp + ".index", data)

    def edit(self, view: View):
        data = {
            'uri': self.uri,
            'data': AdminConfig.get_config(),
        }

        return view.render(self.temp + '.edit', data)

    def update(self, request: Request, response:Response):
        # 有个 copyright() 的函数，避免关键字
        copyright1 = request.input('copyright')
        website = request.input('website')
        bei_an = request.input('bei_an')
        watermark_switch = request.input('watermark_switch')

        b, m = MyValidator.between(copyright1, 1, 100)

        if b is False:
            return response.json({'code': 0, 'message': '版权信息' + m})
        b2, m = MyValidator.ch_alpha_num(copyright1)
        if b2 and b:
            AdminConfig.where('title', 'copyright').update({'content': copyright1})
        else:
            return response.json({'code': 0, 'message': '版权信息' + m})

        b, m = MyValidator.between(website, 1, 100)
        if b is False:
            return response.json({'code': 0, 'message': '网站名称' + m})
        b2, m = MyValidator.ch_alpha_num(website)
        if b and b2:
            AdminConfig.where('title', 'website').update({'content': website})
        else:
            return response.json({'code': 0, 'message': '网站名称' + m})

        b, m = MyValidator.between(bei_an, 1, 100)
        if b is False:
            return response.json({'code': 0, 'message': '备案号' + m})
        b2, m = MyValidator.ch_alpha_num(bei_an)
        if b and b2:
            AdminConfig.where('title', 'bei_an').update({'content': bei_an})
        else:
            return response.json({'code': 0, 'message': '备案号' + m})

        b, m = MyValidator.boolean(watermark_switch)
        if b:
            AdminConfig.where('title', 'watermark_switch').update({'content': watermark_switch})
            Session.set('watermark_switch', watermark_switch)
        else:
            return response.json({'code': 0, 'message': '屏幕水印' + m})

        remark = MyControllerTrait.get_permission_title(request.get_path())
        AdminActionLog.generate_log(Session.get('admin_user')['id'], request.get_path(), request.ip(), remark)
        return response.json({'code': 1, 'message': '成功'})
